/* A Unix wc utility implemented by Flex */
/* support input from file */
%option noyywrap
%{
#include <stdio.h>

int chars = 0;
int lines = 0;
int words = 0;
%}

%%

[^ \n\t\r\f\v]+			{words++; chars+=strlen(yytext);}
\n									{chars++; lines++;}
.										{chars++;}

%%

int main(int argc, char **argv) {
	int i;
	if(argc < 2) {
		yylex();
		printf("%8d%8d%8d\n", lines, words, chars);
		return 0;
	}

	for(i = 1; i < argc; ++i) {
		FILE *f = fopen(argv[i], "r");

		if(!f) {
			perror(argv[i]);
			return 1;
		}

		yyrestart(f);
		yylex();
		fclose(f);
		printf("%8d%8d%8d %s\n", lines, words, chars, argv[i]);
		lines = words = chars = 0;
	}

	return 0;
}
